import os
import json
import sys

CUR_PATH, _ = os.path.split(os.path.abspath(__file__))

def merge(date):

    with open(os.path.join(CUR_PATH, "stat/%s/cantian_count_comment.json" % date)) as f:
        cantian_count_comment =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/cantian_count_commented.json" % date)) as f:
        cantian_count_commented =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/cantian_code_stat.json" % date)) as f:
        cantian_cantian_code_stat =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/mysql_connector_count_comment.json" % date)) as f:
        mysql_connector_count_comment =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/mysql_connector_count_commented.json" % date)) as f:
        mysql_connector_count_commented =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/mysql_connector_code_stat.json" % date)) as f:
        mysql_connector_code_stat =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/cantian_count_pr.json" % date)) as f:
        cantian_count_pr =json.load(f)
    with open(os.path.join(CUR_PATH, "stat/%s/mysql_connector_count_pr.json" % date)) as f:
        mysql_connector_count_pr =json.load(f)

    merged = []
    users = {}
    for one in cantian_count_comment:
        users[one['name']] = {'additions':0, "deletions": 0, "comment": one['comment_count'], "commented": 0, "pr_count": 0}
    for one in cantian_count_commented:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data["commented"] += one["commented_count"]
        users[one['name']] = data
    for one in cantian_cantian_code_stat:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data['additions'] += one['additions']
        data["deletions"] += one["deletions"]
        users[one['name']] = data
    for one in mysql_connector_count_comment:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data["comment"] += one["comment_count"]
        users[one['name']] = data
    for one in mysql_connector_count_commented:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data["commented"] += one["commented_count"]
        users[one['name']] = data
    for one in mysql_connector_code_stat:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data['additions'] += one['additions']
        data["deletions"] += one["deletions"]
        users[one['name']] = data
    for one in cantian_count_pr:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data["pr_count"] += one["pr_count"]
        users[one['name']] = data
    for one in mysql_connector_count_pr:
        data = users.get(one['name'], {'additions':0, "deletions": 0, "comment": 0, "commented": 0, "pr_count": 0})
        data["pr_count"] += one["pr_count"]
        users[one['name']] = data

    for one in users:
        if one != 'openeuler-ci-bot':
            merged.append({"name": one, "additions": users[one]['additions'], "deletions": users[one]["deletions"], "comment": users[one]["comment"], "commented": users[one]["commented"], "pr_count": users[one]["pr_count"]})

    merged.sort(key = lambda x: x['comment'], reverse=True)
    merged.sort(key = lambda x: x['pr_count'], reverse=True)
    merged.sort(key = lambda x: x['additions'], reverse=True)

    with open("stat/%s/merged.json" % date, "w") as f:
        json.dump(merged, f, indent=4)

if __name__ == '__main__':
    date = sys.argv[1]
    merge(date)
